/**
 * 全站http配置
 *
 * header参数说明
 * serialize是否开启form表单提交
 * isToken是否需要token
 */
import axios from 'axios'
// import store from '@/store';
// import router from '@/router/router'
import { Message } from 'element-ui'
// import website from '@/const/website';
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { baseUrl } from '../config/env.js';

axios.defaults.timeout = 200000;
// axios.defaults.baseURL = baseUrl;
//返回其他状态吗
axios.defaults.validateStatus = function(status) {
    return status >= 200 && status <= 500; // 默认的
};
//跨域请求，允许保存cookie
axios.defaults.withCredentials = true;
// NProgress Configuration
NProgress.configure({
    showSpinner: false
});
//HTTP request拦截
axios.interceptors.request.use(config => {
    
    // 判断经过共享交换平台的，需要token鉴权
    if(config.url.indexOf("api-gateway/gateway") >= 0) {
        config.headers.Authorization =  "Basic "+ sessionStorage.getItem("authToken")  //存储token
    }

    NProgress.start() // start progress bar
    return config
}, error => {
    return Promise.reject(error)
});
//HTTP response拦截
axios.interceptors.response.use(res => {
    NProgress.done();
    let status = Number(res.status) || 200;
    if (status == 200) {
        status = res.data.code;
    }
    console.log(res.status)
    // const statusWhiteList = website.statusWhiteList || [];
    const message = res.data.msg || '未知错误';
    //如果是401则跳转到登录页面
    // if (status === 401) {
    //     let value;
	// 	if (window.location !== window.parent.location) {
	// 		value = window.parent.location.href.split("#")[1];
	// 	} else {
	// 		value = window.location.href.split("#")[1];
	// 	}
    //     saveRouteHash(value);
    //     store.dispatch('FedLogOut').then(() => router.push({ path: '/login' }));
    //     return res;
    // }

    //如果请求为200则放过，否者默认统一处理,或者在website中配置statusWhiteList白名单自行处理
    if (res.status !== 200 ) {
        Message({
            message: message,
            type: 'error'
        })
        return Promise.reject(new Error(message))
    }

    // 如果是白名单类型放入catch自行处理
    if (res.status !== 200) return Promise.reject(res);
    return res;
}, error => {
    console.log(error);
    NProgress.done();
    return Promise.reject(new Error(error));
})

export default axios;
